home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 June: Reference Library / Dev.CD Jun 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / 3d game controls / source / camera.c < prev    next >
Encoding:
Text File  |  1996-10-04  |  10.8 KB  |  322 lines

  1. //--------------------------------------------------------------------------------------------
  2. //  Camera Functions
  3. //
  4. //      by Philip McBride
  5. //
  6. //--------------------------------------------------------------------------------------------
  7.  
  8.  
  9. #include <FixMath.h>
  10. #include "GameControls.h"
  11. #include "extern.h"
  12. #include "camera.h"
  13.  
  14. //--------------------------------------------------------------------------------------------
  15. //  Create a Camera (with default settings) -- used if no camera included in file
  16. //
  17. TQ3CameraObject MyNewCamera(CWindowPtr theWindow)
  18. {
  19.     TQ3ViewAngleAspectCameraData    perspectiveData;
  20.     TQ3CameraObject                    camera;
  21.     float                             fieldOfView = .50;
  22.     TQ3Status                        returnVal = kQ3Failure ;
  23.  
  24.     // Assign default placement and range
  25.     perspectiveData.cameraData.placement.cameraLocation     = kMyDefaultFrom;
  26.     perspectiveData.cameraData.placement.pointOfInterest     = kMyDefaultTo;
  27.     perspectiveData.cameraData.placement.upVector             = kMyDefaultUp;
  28.     perspectiveData.cameraData.range.hither    = kMyDefaultHither;
  29.     perspectiveData.cameraData.range.yon     = kMyDefaultYon;
  30.  
  31.     // Assign standard viewport
  32.     perspectiveData.cameraData.viewPort.origin.x = -1.0;
  33.     perspectiveData.cameraData.viewPort.origin.y = 1.0;
  34.     perspectiveData.cameraData.viewPort.width = 2.0;
  35.     perspectiveData.cameraData.viewPort.height = 2.0;
  36.     
  37.     perspectiveData.fov                = fieldOfView;
  38.     perspectiveData.aspectRatioXToY    =
  39.         (float) (theWindow->portRect.right - theWindow->portRect.left) / 
  40.         (float) (theWindow->portRect.bottom - theWindow->portRect.top);
  41.         
  42.     camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
  43.     
  44.     return camera ;
  45. }
  46.  
  47. //--------------------------------------------------------------------------------------------
  48. //  Get a Model's Bounding Box
  49. //
  50. void MyGetBoundingBox(DocumentPtr theDocument, TQ3GroupObject mainGroup, TQ3BoundingBox *viewBBox)
  51. {
  52.     TQ3Status                    status;
  53.     TQ3ViewObject                 viewObject = theDocument->theView;
  54.     
  55.     Q3View_StartBoundingBox(viewObject,kQ3ComputeBoundsApproximate);
  56.     do {
  57.         status = Q3DisplayGroup_Submit(mainGroup, viewObject);
  58.     } while (Q3View_EndBoundingBox(viewObject, viewBBox) == kQ3ViewStatusRetraverse);                                        
  59. }
  60.  
  61. //--------------------------------------------------------------------------------------------
  62. //  Get the Camera Data (initialize for new file and camera)
  63. //
  64. // Get the camera information from the current view and initialize the
  65. // camera related fields of the document record
  66. void MyGetCameraData(DocumentPtr theDocument, TQ3CameraObject theCamera)
  67. {
  68.     TQ3CameraPlacement    cameraPlacement;
  69.  
  70.     // Get the camera data.
  71.     Q3Camera_GetPlacement(theCamera, &cameraPlacement);
  72.  
  73.     // Set the document's camera data.
  74.     theDocument->cameraLocation = cameraPlacement. cameraLocation;
  75.     theDocument->pointOfInterest = cameraPlacement. pointOfInterest;
  76.     theDocument->yVector = cameraPlacement. upVector;
  77.  
  78.     // Calculate the x and z vectors and assign them to the document.
  79.     Q3Point3D_Subtract(&theDocument->pointOfInterest,
  80.         &theDocument->cameraLocation, &theDocument->zVector);
  81.     Q3Vector3D_Cross(&theDocument->zVector, &theDocument->yVector,
  82.         &theDocument->xVector);
  83.     
  84.     Q3Vector3D_Normalize(&theDocument->xVector, &theDocument->xVector);
  85. }
  86.  
  87. //--------------------------------------------------------------------------------------------
  88. //  Set the Camera'a Data (when moved)
  89. //
  90. // Set the camera of the current view to the values in the document record
  91. void MySetCameraData(DocumentPtr theDocument, TQ3CameraObject theCamera)
  92. {
  93.     TQ3CameraPlacement    cameraPlacement;
  94.  
  95.     // Set the camera placement data.
  96.     cameraPlacement.cameraLocation = theDocument->cameraLocation;
  97.     cameraPlacement.pointOfInterest = theDocument->pointOfInterest;
  98.     cameraPlacement.upVector = theDocument->yVector;
  99.  
  100.     // Set the camera data to the camera.
  101.     Q3Camera_SetPlacement(theCamera, &cameraPlacement);
  102. }
  103.  
  104. //--------------------------------------------------------------------------------------------
  105. //  Move Camera X
  106. //
  107. // Move the camera along the X axis
  108. void MyMoveCameraX(DocumentPtr theDocument, float dX)
  109. {
  110.     TQ3ViewObject        theView;
  111.     TQ3CameraObject        theCamera;
  112.     TQ3Vector3D            scaledVector;
  113.     TQ3Point3D            newPoint;
  114.  
  115.     // Get the view and the camera objects.
  116.     theView = theDocument->theView;
  117.     Q3View_GetCamera(theView, &theCamera);
  118.  
  119.     // Scale the X vector to make it dX longer.
  120.     Q3Vector3D_Scale(&theDocument->xVector,dX/Q3Vector3D_Length(&theDocument->xVector),
  121.                         &scaledVector);
  122.  
  123.     // Move the camera position and direction by the new vector.
  124.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
  125.     theDocument->cameraLocation = newPoint;
  126.     Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
  127.     theDocument->pointOfInterest = newPoint;
  128.  
  129.     // Set the updated camera data to the camera.
  130.     MySetCameraData(theDocument, theCamera);
  131.  
  132.     // Update the view with the changed camera and dispose of the camera.
  133.     Q3View_SetCamera(theView, theCamera);
  134.     Q3Object_Dispose(theCamera);
  135. }
  136.  
  137. //--------------------------------------------------------------------------------------------
  138. //  Move Camera Y
  139. //
  140. // Move the camera along the Y axis
  141. void MyMoveCameraY(DocumentPtr theDocument, float dY)
  142. {
  143.     TQ3ViewObject        theView;
  144.     TQ3CameraObject        theCamera;
  145.     TQ3Vector3D            scaledVector;
  146.     TQ3Point3D            newPoint;
  147.  
  148.     // Get the view and the camera objects.
  149.     theView = theDocument->theView;
  150.     Q3View_GetCamera(theView, &theCamera);
  151.  
  152.     // Scale the Y vector to make it dY longer.
  153.     Q3Vector3D_Scale(&theDocument->yVector,dY/Q3Vector3D_Length(&theDocument->yVector),
  154.                         &scaledVector);
  155.  
  156.     // Move the camera position and direction by the new vector.
  157.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
  158.     theDocument->cameraLocation = newPoint;
  159.     Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
  160.     theDocument->pointOfInterest = newPoint;
  161.  
  162.     // Set the updated camera data to the camera.
  163.     MySetCameraData(theDocument, theCamera);
  164.  
  165.     // Update the view with the changed camera and dispose of the camera.
  166.     Q3View_SetCamera(theView, theCamera);
  167.     Q3Object_Dispose(theCamera);
  168. }
  169.  
  170. //--------------------------------------------------------------------------------------------
  171. //  Move Camera Z
  172. //
  173. // Move the camera along the Z axis
  174. void MyMoveCameraZ(DocumentPtr theDocument, float dZ)
  175. {
  176.     TQ3ViewObject        theView;
  177.     TQ3CameraObject        theCamera;
  178.     TQ3Vector3D            scaledVector;
  179.     TQ3Point3D            newPoint;
  180.  
  181.     // Get the view and the camera objects.
  182.     theView = theDocument->theView;
  183.     Q3View_GetCamera(theView, &theCamera);
  184.  
  185.     // Scale the Z vector to make it dZ longer.
  186.     Q3Vector3D_Scale(&theDocument->zVector,dZ/Q3Vector3D_Length(&theDocument->zVector),
  187.                         &scaledVector);
  188.  
  189.     // Move the camera position and direction by the new vector.
  190.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
  191.     theDocument->cameraLocation = newPoint;
  192.     Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
  193.     theDocument->pointOfInterest = newPoint;
  194.     
  195.     // Set the updated camera data to the camera.
  196.     MySetCameraData(theDocument, theCamera);
  197.  
  198.     // Update the view with the changed camera and dispose of the camera.
  199.     Q3View_SetCamera(theView, theCamera);
  200.     Q3Object_Dispose(theCamera);
  201. }
  202.  
  203. //--------------------------------------------------------------------------------------------
  204. //  Rotate Camera X
  205. //
  206. // Rotate the camera about the X axis
  207. void MyRotateCameraX(DocumentPtr theDocument, float dX)
  208. {
  209.     TQ3ViewObject        theView;
  210.     TQ3CameraObject        theCamera;
  211.     TQ3Vector3D            rotatedVector;
  212.     TQ3Matrix4x4        rotationMatrix;
  213.  
  214.     // Get the view and the camera objects.
  215.     theView = theDocument->theView;
  216.     Q3View_GetCamera(theView, &theCamera);
  217.  
  218.     // Create the rotation matrix for rotating about the x axis.
  219.     Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
  220.         &theDocument->cameraLocation, &theDocument->xVector, dX);
  221.  
  222.     // Rotate the y vector (up vector) about the x axis.
  223.     Q3Vector3D_Transform(&theDocument->yVector, &rotationMatrix,
  224.         &rotatedVector);
  225.     theDocument->yVector = rotatedVector;
  226.  
  227.     // Rotate the z vector about the x axis.
  228.     Q3Vector3D_Transform(&theDocument->zVector, &rotationMatrix,
  229.         &rotatedVector);
  230.     theDocument->zVector = rotatedVector;
  231.  
  232.     // Update the point of interest from the new z vector.
  233.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,
  234.         &theDocument->zVector, &theDocument->pointOfInterest);
  235.  
  236.     // Set the updated camera data to the camera.
  237.     MySetCameraData(theDocument, theCamera);
  238.  
  239.     // Update the view with the changed camera and dispose of the camera.
  240.     Q3View_SetCamera(theView, theCamera);
  241.     Q3Object_Dispose(theCamera);
  242. }
  243.  
  244. //--------------------------------------------------------------------------------------------
  245. //  Rotate Camera Y
  246. //
  247. // Rotate the camera about the Y axis
  248. void MyRotateCameraY(DocumentPtr theDocument, float dY)
  249. {
  250.     TQ3ViewObject        theView;
  251.     TQ3CameraObject        theCamera;
  252.     TQ3Vector3D            rotatedVector;
  253.     TQ3Matrix4x4        rotationMatrix;
  254.  
  255.     // Get the view and the camera objects.
  256.     theView = theDocument->theView;
  257.     Q3View_GetCamera(theView, &theCamera);
  258.  
  259.     // Create the rotation matrix for rotating about the y axis.
  260.     Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
  261.         &theDocument->cameraLocation, &theDocument->yVector, dY);
  262.  
  263.     // Rotate the z vector about the y axis.
  264.     Q3Vector3D_Transform(&theDocument->zVector, &rotationMatrix,
  265.         &rotatedVector);
  266.     theDocument->zVector = rotatedVector;
  267.  
  268.     // Rotate the x vector about the y axis.
  269.     Q3Vector3D_Transform(&theDocument->xVector, &rotationMatrix,
  270.         &rotatedVector);
  271.     theDocument->xVector = rotatedVector;
  272.  
  273.     // Update the point of interest from the new z vector.
  274.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,
  275.         &theDocument->zVector, &theDocument->pointOfInterest);
  276.  
  277.     // Set the updated camera data to the camera.
  278.     MySetCameraData(theDocument, theCamera);
  279.  
  280.     // Update the view with the changed camera and dispose of the camera.
  281.     Q3View_SetCamera(theView, theCamera);
  282.     Q3Object_Dispose(theCamera);
  283. }
  284.  
  285. //--------------------------------------------------------------------------------------------
  286. //  Rotate Camera Z
  287. //
  288. // Rotate the camera about the Z axis
  289. void MyRotateCameraZ(DocumentPtr theDocument, float dZ)
  290. {
  291.     TQ3ViewObject        theView;
  292.     TQ3CameraObject        theCamera;
  293.     TQ3Vector3D            rotatedVector;
  294.     TQ3Matrix4x4        rotationMatrix;
  295.  
  296.     // Get the view and the camera objects.
  297.     theView = theDocument->theView;
  298.     Q3View_GetCamera(theView, &theCamera);
  299.  
  300.     // Create the rotation matrix for rotating about the z axis.
  301.     Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
  302.         &theDocument->cameraLocation, &theDocument->zVector, dZ);
  303.  
  304.     // Rotate the y vector (up vector) about the z axis.
  305.     Q3Vector3D_Transform(&theDocument->yVector, &rotationMatrix,
  306.         &rotatedVector);
  307.     theDocument->yVector = rotatedVector;
  308.  
  309.     // Rotate the x vector about the z axis.
  310.     Q3Vector3D_Transform(&theDocument->xVector, &rotationMatrix,
  311.         &rotatedVector);
  312.     theDocument->xVector = rotatedVector;
  313.  
  314.     // Set the updated camera data to the camera.
  315.     MySetCameraData(theDocument, theCamera);
  316.  
  317.     // Update the view with the changed camera and dispose of the camera.
  318.     Q3View_SetCamera(theView, theCamera);
  319.     Q3Object_Dispose(theCamera);
  320. }
  321.  
  322.